package com.entor.hqj.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.entor.hqj.main.IndexActivity;
import com.entor.hqj.model.User;
import com.entor.hqj.util.HttpPostUtils;

public class LoginActivity extends Activity {

    private String result;
    private Button button;
    private TextView regTv;
    private EditText username;
    private EditText password;
    public static User userInfo = new User();
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message message){
            result = (String)message.obj;
            JSONObject resultJson =
                    JSON.parseObject(result);
            if(resultJson!=null) {
                String msg = resultJson.getString("msg");
                boolean sus = resultJson.getBoolean("success");
                userInfo = resultJson.getObject("obj", User.class);
                System.out.println("结果是" + msg);
                if (sus) {
                    Intent intent = new Intent(LoginActivity.this, IndexActivity.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
                }
            }else{
                Toast.makeText(LoginActivity.this, "网络链接超时", Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        button = (Button) findViewById(R.id.btn_login);
        regTv = (TextView) findViewById(R.id.tv_reg);
        username = (EditText) findViewById(R.id.et_userName);
        password = (EditText) findViewById(R.id.et_password);
        MyListenner listenner = new MyListenner();
        RegListener regListener = new RegListener();
        //按钮绑定监听器
        button.setOnClickListener(listenner);
        regTv.setOnClickListener(regListener);
    }
    //按钮的监听器是一个接口，所有不能直接创建对象
    class MyListenner implements View.OnClickListener{
        //当控件（在这里就是按钮）被点击当时候，会调用的方法
        //v：事件源（谁触发了点击这个事件，谁就是事件源）   指代：当前被点击的按钮
        @Override
        public void onClick(View v){
            String uname = username.getText().toString().trim();//获取用户名输入的值
            String pwd = password.getText().toString().trim();//获取密码输入的值
            //做传参数的字符串拼接
            final String user = "loginname="+uname+"&password="+pwd;
            if(uname!= null && !uname.equals("") && pwd!=null && !pwd.equals("")) {
                /*这里需要留意的是httpPostUtils请求在Android里面不能放在主线程里面，
                必须新建一个子线程，然后通过Hanlder把子线程的值传过来更新UI（因为子线程不能直接更改UI）*/
                new Thread(){
                    public void run(){
                        //调用请求并获取结果
                        String response =  HttpPostUtils.doPostRequest("admin/login",user);
                        Message message = Message.obtain();
                        message.obj = response;
                        mHandler.sendMessage(message);
                    }
                }.start();
            }else {
                System.out.println("用户名或密码不能为空");
                /*
                context: 调用的上下文，通常为Application或Activity对象
                text: 显示的消息
                duration: 显示的时间长短，为 Toast.LENGTH_LONG或Toast.LENGTH_SHORT
                */
                Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();

            }
            //button.setText("按钮"+uname);
        }

    }

    class RegListener implements View.OnClickListener{
        @Override
        public void onClick(View v){
            //regbtn.setText("按钮");
            Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
            startActivity(intent);
        }
    }
}
